package edu.byu.intex2.data.bo;

import edu.byu.intex2.data.Controller;
import edu.byu.intex2.data.DataException;
import edu.byu.intex2.data.dao.ServiceDAO;
import edu.byu.intex2.data.dao.ServiceLineDAO;
import edu.byu.intex2.data.dao.TaskCCDAO;
import edu.byu.intex2.data.dao.TaskDAO;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Jason Whiting
 */
public class ServiceLine extends BusinessObject
{

    private double costOfLabor;
    private double costOfParts;
    private String serviceId;
    private Service service;
    private String taskId;
    private Task task;
    private List<PartServiceLine> pslList = new ArrayList<PartServiceLine>();

    /**
     * Creates a new instance of BusinessObject
     * @param id
     */
    public ServiceLine(String id)
    {
        super(id);
    }//constructor

    //Quick Access Method
    public void save() throws DataException
    {
        try
        {
            ServiceLineDAO.getInstance().save(this);
        } catch (DataException e)
        {
            e.printStackTrace();
        }
    }

    /**
     * @return the costOfLabor
     */
    public double getCostOfLabor()
    {
        return costOfLabor;
    }

    /**
     * @param costOfLabor the costOfLabor to set
     */
    public void setCostOfLabor(double costOfLabor)
    {
        this.costOfLabor = costOfLabor;
        this.setDirty();
    }

    /**
     * @return the costOfParts
     */
    public double getCostOfParts()
    {
        return costOfParts;
    }

    /**
     * @param costOfLabor the costOfLabor to set
     */
    public void setCostOfParts(double costOfParts)
    {
        this.costOfParts = costOfParts;
        this.setDirty();
    }

    /**
     * @return the serviceId
     */
    public String getServiceId()
    {
        return serviceId;
    }

    /**
     * @param serviceId the serviceId to set
     */
    public void setServiceId(String serviceId)
    {
        this.serviceId = serviceId;
        this.setDirty();
    }

    /**
     * @return the service from the DAO
     */
    public Service getService()
    {
        if (service == null)
        {
            try
            {
                service = ServiceDAO.getInstance().read(serviceId);
            } catch (Exception e)
            {
                e.printStackTrace();
            }
        }
        return service;
    }

    /**
     * @param service the service to set
     */
    public void setService(Service service)
    {
        this.service = service;
    }

    /**
     * @return the taskId
     */
    public String getTaskId()
    {
        return taskId;
    }

    /**
     * @param taskId the taskId to set
     */
    public void setTaskId(String taskId)
    {
        this.taskId = taskId;
        this.setDirty();
    }

    /**
     * @return the task from the DAO
     */
    public Task getTask()
    {
        if (task == null)
        {
            try
            {
                task = TaskDAO.getInstance().read(taskId);
            } catch (Exception e)
            {
                e.printStackTrace();
            }
        }
        return task;
    }

    /**
     * @param task the task to set
     */
    public void setTask(Task task)
    {
        this.task = task;
        this.setDirty();
    }

    /**
     * @return the pslList
     */
    public List<PartServiceLine> getPslList()
    {
        return pslList;
    }

    /**
     * @param partsList the partsList to set
     */
    public void setPslList(List<PartServiceLine> pslList)
    {
        this.pslList = pslList;
        this.setDirty();
    }

    /**
     * @param psl to add to pslList
     */
    public void addPslToList(PartServiceLine psl)
    {
        this.pslList.add(psl);
    }

    @Override
    public String toString()
    {
        Double total = Controller.roundTwoDecimals(costOfLabor + costOfParts);
        return getTask().getDescription() + " - $" + total.toString();
    }

    // Quick Method to get labor hours from TaskCC
    public Double getLaborHours()
    {
        Double laborHours = 0.0;
        try
        {
            laborHours = TaskCCDAO.getInstance().read(getTaskId(), getService().getPhysicalCar().getConceptualCarId()).getLaborTime();
        } catch (Exception e)
        {
            e.printStackTrace();
        }
        return laborHours;
    }
}
